//
//  RefundViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "RefundViewController.h"
#import "OrderDetailTableViewCell.h"

@interface RefundViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet SZTextView *contentTextView;
@property (weak, nonatomic) IBOutlet UIView *contentBackView;
@property (weak, nonatomic) IBOutlet UIView *contentTotalPriceView;

@end

@implementation RefundViewController

#pragma mark - IBAction
- (IBAction)submitButtonAction:(id)sender {
    if (!TextIsNotNil(self.contentTextView.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请填写退货原因" animated:YES];
        return;
    }
    
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Save_Refund parameters:@{@"omid":self.detailModel.omid,@"rcontent":self.contentTextView.text,@"rprice":self.detailModel.omprice} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"申请已提交" animated:YES];
            [self popToRootViewController];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"申请退款";
    [self createUI];
}

#pragma mark - action
- (void)createUI {
    [self.tableView clearExtendCellLine];
    
    OrderDetailTableViewCell *detailCellFirstView = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetailTableViewCell" owner:self options:nil] objectAtIndex:0];
    [detailCellFirstView.contentView setFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    [detailCellFirstView setBackgroundColor:[UIColor whiteColor]];
    [detailCellFirstView.firstLeftLabel setText:@"退款金额"];
    [detailCellFirstView.firstRightLabel setText:[NSString stringWithFormat:@"¥%.2f",self.detailModel.omprice.floatValue]];
    [self.contentTotalPriceView addSubview:detailCellFirstView.contentView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.detailModel.orderson.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"refundViewCellIdentifiey"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetailTableViewCell" owner:self options:nil] objectAtIndex:2];
    }
    
    OrdersonModel *model = self.detailModel.orderson[indexPath.row];
    [cell.thirdImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImagePictures];
    [cell.thirdNameLabel setText:model.gname];
    [cell.thirdPriceLabel setText:[NSString stringWithFormat:@"¥%.2f",model.osprice.floatValue]];
    [cell.thirdCountLabel setText:[NSString stringWithFormat:@"x%d",model.osnumber.intValue]];
    //MARK:测试 商品属性
    NSString *specs = @"";
    for (NSInteger i = 0; i < model.otops.count; i++) {
        OtopsModel *twoAtt = model.otops[i];
        if (i == 0) {
            specs = twoAtt.taname;
        } else {
            specs = [NSString stringWithFormat:@"%@;%@",specs,twoAtt.taname];
        }
    }
    [cell.thirdSpecsLabel setText:specs];
    
    return cell;
}
#pragma mark - UITableViewDelegate

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
